package cn.hzking.gjmall.activitys.second;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.hzking.gjmall.R;
import cn.hzking.gjmall.adpters.search.SearchHotWordAdater;
import cn.hzking.gjmall.base.ColorStatuBarActivity;
import cn.hzking.gjmall.bean.SearchHotWordBean;

public class SearchActivity extends ColorStatuBarActivity {

    @Bind(R.id.search_et)
    EditText searchEt;
    @Bind(R.id.btn_search)
    TextView btnSearch;
    @Bind(R.id.search_hotword_rv)
    RecyclerView searchHotwordRv;
    @Bind(R.id.back_search)
    RelativeLayout backSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
        initRv();
        BindEt();

    }

    private void BindEt() {
        searchEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(!TextUtils.isEmpty(s)){
                    btnSearch.setEnabled(true);
                    btnSearch.setSelected(true);
                }else {
                    btnSearch.setEnabled(false);
                    btnSearch.setSelected(false);
                }

            }
        });
    }

    private void initRv() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 5);
        searchHotwordRv.setLayoutManager(gridLayoutManager);
        SearchHotWordAdater searchHotWordAdater = new SearchHotWordAdater(R.layout.item_search_hotword, null);
        searchHotwordRv.setAdapter(searchHotWordAdater);
        String[] strings = {"口罩", "面膜", "口红", "洗面奶",
                "手表", "酵素", "保湿", "气垫BB", "兰芝", "休闲零食"};
        List<SearchHotWordBean> list = new ArrayList<>();
        for (int i = 0; i < strings.length; i++) {
            SearchHotWordBean searchHotWordBean = new SearchHotWordBean(strings[i]);
            list.add(searchHotWordBean);
        }
        searchHotWordAdater.setNewData(list);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    //返回按钮
    @OnClick(R.id.back_search)
    public void back(View view){
        finish();
    }

    @OnClick(R.id.btn_search)
    public void search(View view){
        Intent intent = new Intent(this,SearchGoodListActivity.class);
        intent.putExtra("keyword",searchEt.getText().toString());
        startActivity(intent);
    }
}
